/**
 * Created by kule on 2017/5/17.
 */
export const cacheWithClean=function(fn,{
    maxAge=null
}={}){
    let lastTime=0;
    let rst;
    const clean=()=>{
        if(maxAge){
            lastTime=0;
        }
        rst=undefined;
    };
    const cached=()=>{
        if(!maxAge){
            if(rst===undefined){
                rst=fn.call(this);
            }
            return rst;
        }

        const now=(new Date).getTime();
        if(now-lastTime>maxAge){
            rst=fn.call(this);
            lastTime=now;
        }
        return rst;
    };
    return {
        cached,
        clean
    }
};
export const cache=function(fn,{
    maxAge=null
}={}){
    return cacheWithClean(fn,{
        maxAge
    }).cached;
};
export default cache;